//
//  YZMStockHandicapView.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/11/8.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMStockHandicapView.h"
#import "NSNumber+Utils.h"

@interface YZMStockHandicapView ()

@property (nonatomic, strong) NSMutableArray *contentArray;

@end

@implementation YZMStockHandicapView {
    CGFloat _type;
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        self.backgroundColor = [YZMAppStyle colorForMain];
        [self createObjects];
    }
    
    return self;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    self.backgroundColor = [YZMAppStyle colorForMain];
    [self createObjects];
}

- (instancetype)initWithType:(NSUInteger)index {
    if (self = [super init]) {
        _type = index;
        self.backgroundColor = [YZMAppStyle colorForMain];
        [self createOBJ];
    }
    return self;
}

#pragma mark -- ui

- (void)createOBJ {
    
    NSArray *titleArray = @[@"今开", @"昨收", @"振幅", @"涨跌" ,@"涨停", @"跌停", @"最高",  @"最低", @"成交量", @"成交额"];
    if (self.noDailylimitOrDown) {
        titleArray = @[@"今开", @"昨收", @"振幅", @"涨跌", @"最高",  @"最低", @"成交量", @"成交额"];
    }
    self.contentArray = [NSMutableArray arrayWithCapacity:titleArray.count];
    
    CGFloat viewHeight = 50;
    CGFloat viewWidth = ScreenWidth / 2 - 15;
    for (int i = 0; i < titleArray.count; i++) {
        NSString* title = titleArray[i];
        int index = i / 2;
        if (!(i % 2)) {
            UIView* view = [self createLeftView:title value:@"--"];
            view.frame = CGRectMake(15, index * viewHeight, viewWidth, viewHeight);
            [self addSubview:view];
//            [view mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.left.equalTo(self).offset(15);
//                make.top.equalTo(self).offset(index * viewHeight);
//                make.height.mas_offset(viewHeight);
//                make.width.mas_offset(viewWidth);
//            }];
        } else {
            UIView* view = [self createRightView:title value:@"--"];
            view.frame = CGRectMake(ScreenWidth / 2,index * viewHeight, viewWidth, viewHeight);
            [self addSubview:view];
//            [view mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.right.equalTo(self).offset(-15);
//                make.top.equalTo(self).offset(index * viewHeight);
//                make.height.mas_offset(viewHeight);
//                make.width.mas_offset(viewWidth);
//            }];
        }
    }
}

- (UIView *)createLeftView:(NSString *)title value:(NSString *)value{
    
    UIView* view = [[UIView alloc] init];
    
    UIView* sp = [YZMAppStyle splitLine];
    [view addSubview:sp];
    [sp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(view);
    }];
    
    UIView* sp2 = [YZMAppStyle splitLine];
    [view addSubview:sp2];
    [sp2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(view);
        make.height.equalTo(view);
        make.width.mas_equalTo(0.5);
    }];
    
    UILabel *titleLB = [YZMAppStyle labelWithSmallLightGray];
    titleLB.text = title;
    titleLB.textAlignment = NSTextAlignmentLeft;
    [view addSubview:titleLB];
    [titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(view);
    }];
    
    UILabel* valueLB = [YZMAppStyle labelWithSmallLightGray];
    valueLB.text = value;
    valueLB.textAlignment = NSTextAlignmentRight;
    [view addSubview:valueLB];
    [valueLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(view);
        make.left.equalTo(titleLB.mas_right);
        make.right.equalTo(view).offset(-15);
    }];
    
    [self.contentArray addObject:valueLB];
    
    return view;
}

- (UIView *)createRightView:(NSString *)title value:(NSString *)value{
    
    UIView* view = [[UIView alloc] init];
    
    UIView* sp = [YZMAppStyle splitLine];
    [view addSubview:sp];
    [sp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(view);
    }];
    
    
    UILabel *titleLB = [YZMAppStyle labelWithSmallLightGray];
    titleLB.text = title;
    titleLB.textAlignment = NSTextAlignmentLeft;
    [view addSubview:titleLB];
    [titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(view);
        make.left.equalTo(view).offset(15);
    }];
    
    UILabel* valueLB = [YZMAppStyle labelWithSmallLightGray];
    valueLB.text = value;
    valueLB.textAlignment = NSTextAlignmentRight;
    [view addSubview:valueLB];
    [valueLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(view);
        make.left.equalTo(titleLB.mas_right);
    }];
    
    [self.contentArray addObject:valueLB];
    
    return view;
}


- (void)createObjects{

    WS(ws)
    
    NSArray *titleArray = @[@"今开", @"昨收", @"最高",  @"最低", @"涨停", @"跌停", @"振幅", @"成交量", @"成交额"];
    if (self.noDailylimitOrDown) {
        titleArray = @[@"今开", @"昨收", @"最高", @"最低", @"振幅", @"成交量", @"成交额"];
    }
    UILabel *firstLastLB = nil;
    UILabel *secondLastLB = nil;
    UILabel *thirdLastLB = nil;
    NSMutableArray *titleLBArray = [NSMutableArray arrayWithCapacity:titleArray.count];
    for (int i = 0; i < titleArray.count; i++) {
        
        UILabel *titleLB = [YZMAppStyle labelWithSmallLightGray];
        titleLB.text = [NSString stringWithFormat:@"%@", titleArray[i]];
        titleLB.textAlignment = NSTextAlignmentLeft;
        [self addSubview:titleLB];
        [titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if (i % 3 == 0) {
                
                make.left.equalTo(ws).offset(25);
            
                if (firstLastLB) {
                    
                    make.top.equalTo(firstLastLB.mas_bottom).offset(15);
                } else {
                    
                    make.top.equalTo(ws).offset(9);
                }
                
            } else if( i %3 == 1) {
                
                make.left.equalTo(firstLastLB.mas_right).offset((ScreenWidth-150)/3);
                
                if (secondLastLB) {
                    
                    make.top.equalTo(secondLastLB.mas_bottom).offset(15);
                } else {
                    
                    make.top.equalTo(ws).offset(9);
                }
            }else{
               
                make.left.equalTo(secondLastLB.mas_left).offset((ScreenWidth-20)/3);
                
                if (thirdLastLB) {
                    
                    make.top.equalTo(thirdLastLB.mas_bottom).offset(15);
                } else {
                    
                    make.top.equalTo(ws).offset(9);
                }
            }
        }];
        
        if (i % 3 == 0) {
            
            firstLastLB = titleLB;
        } else if(i % 3 == 1 ){
            
            secondLastLB = titleLB;
        }else{
            
            thirdLastLB = titleLB;
        }
        
        [titleLBArray addObject:titleLB];
    }
    
    self.contentArray = [NSMutableArray arrayWithCapacity:titleArray.count];
    for (int i = 0; i < titleArray.count; i++) {
        
        UILabel *titleLB = titleLBArray[i];
        UILabel *contentLB = nil;
        
        contentLB = [YZMAppStyle labelWithSmallLightGray];
        
        contentLB.text = [NSString stringWithFormat:@"--"];
        [self addSubview:contentLB];
        [contentLB mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(titleLB.mas_right).offset(2);
            make.centerY.equalTo(titleLB);
        }];
        
        [self.contentArray addObject:contentLB];
    }
    
}

- (void)setRealInfo:(YZMStockRealModel *)realInfo {
    
    if (_type) {
        if ([@(realInfo.price) isGreaterThanDouble:0]) {
            
            //今开
            [self compareValue:@(realInfo.openPrice) withAfter:@(realInfo.lastClosePrice) withIndex:0];
            
            //昨收
            [self compareValue:nil withAfter:@(realInfo.lastClosePrice) withIndex:1];
            
            //最高
            [self compareValue:@(realInfo.highPrice) withAfter:@(realInfo.lastClosePrice) withIndex:6];
            
            //最低
            [self compareValue:@(realInfo.lowPrice) withAfter:@(realInfo.lastClosePrice) withIndex:7];
            
            //涨停
            [self compareValue:@(realInfo.limitUpPrice) withAfter:@(realInfo.lastClosePrice) withIndex:4];
            
            //跌停
            [self compareValue:@(realInfo.limitDownPrice) withAfter:@(realInfo.lastClosePrice) withIndex:5];
            
            //振幅
            [self compareValue:@(realInfo.amplitude) withAfter:@(realInfo.amplitude) withIndex:2];
            UILabel *changeLB = self.contentArray[2];
            changeLB.text = [changeLB.text stringByAppendingString:@"%"];
            
            //成交量
            UILabel *dealNumLB = self.contentArray[8];
            dealNumLB.text =[@(realInfo.todayDealNum).unitP2fString stringByAppendingString:@"手"];
            dealNumLB.textColor = [YZMAppStyle colorForMainTextColor];
            
            //成交额
            UILabel *dealPriceLB = self.contentArray[9];
            dealPriceLB.text = [@(realInfo.todayDealPrice).unitP2fString stringByAppendingString:@"元"];
            dealPriceLB.textColor = [YZMAppStyle colorForMainTextColor];
        
            //涨跌
            double distance = realInfo.price - realInfo.lastClosePrice;
            UILabel* changesLB = self.contentArray[3];
            NSString* price = [NSString stringWithFormat:@"%@",@(distance).p02fString];
            if ([@(distance) isEqualWithDouble:0.0]) {
                
                changesLB.textColor = [YZMAppStyle colorForGrayText];
                changesLB.text = @"+0.00";
                
            }else if([@(distance) isGreaterThanDouble:0.0]){
                
                changesLB.textColor = [YZMAppStyle colorForRiseText];
                changesLB.text = price;
                
            }else{
                changesLB.textColor = [YZMAppStyle colorForDropText];
                changesLB.text = price;
            }
        }
        return;
    }
    if ([@(realInfo.price) isGreaterThanDouble:0]) {
        
        //今开
        [self compareValue:@(realInfo.openPrice) withAfter:@(realInfo.lastClosePrice) withIndex:0];
        
        //昨收
        [self compareValue:nil withAfter:@(realInfo.lastClosePrice) withIndex:1];
        
        //最高
        [self compareValue:@(realInfo.highPrice) withAfter:@(realInfo.lastClosePrice) withIndex:2];
        
        //最低
        [self compareValue:@(realInfo.lowPrice) withAfter:@(realInfo.lastClosePrice) withIndex:3];
        if (self.noDailylimitOrDown) {
//            //涨停
//            [self compareValue:@(realInfo.limitUpPrice) withAfter:@(realInfo.lastClosePrice) withIndex:4];
//
//            //跌停
//            [self compareValue:@(realInfo.limitDownPrice) withAfter:@(realInfo.lastClosePrice) withIndex:5];
            
            //振幅
            UILabel *changeLB = self.contentArray[4];
            changeLB.text = [NSString stringWithFormat:@"%@%%", @(realInfo.amplitude).p02fString];
            changeLB.textColor = [YZMAppStyle colorForMainTextColor];
            
            //成交量
            UILabel *dealNumLB = self.contentArray[5];
            dealNumLB.text =[@(realInfo.todayDealNum).unitP2fString stringByAppendingString:@"手"];
            dealNumLB.textColor = [YZMAppStyle colorForMainTextColor];
            
            //成交额
            UILabel *dealPriceLB = self.contentArray[6];
            dealPriceLB.text = [@(realInfo.todayDealPrice).unitP2fString stringByAppendingString:@"元"];
            dealPriceLB.textColor = [YZMAppStyle colorForMainTextColor];
        }else{
            //涨停
            [self compareValue:@(realInfo.limitUpPrice) withAfter:@(realInfo.lastClosePrice) withIndex:4];
            
            //跌停
            [self compareValue:@(realInfo.limitDownPrice) withAfter:@(realInfo.lastClosePrice) withIndex:5];
            
            //振幅
            UILabel *changeLB = self.contentArray[6];
            changeLB.text = [NSString stringWithFormat:@"%@%%", @(realInfo.amplitude).p02fString];
            changeLB.textColor = [YZMAppStyle colorForMainTextColor];
            
            //成交量
            UILabel *dealNumLB = self.contentArray[7];
            dealNumLB.text =[@(realInfo.todayDealNum).unitP2fString stringByAppendingString:@"手"];
            dealNumLB.textColor = [YZMAppStyle colorForMainTextColor];
            
            //成交额
            UILabel *dealPriceLB = self.contentArray[8];
            dealPriceLB.text = [@(realInfo.todayDealPrice).unitP2fString stringByAppendingString:@"元"];
            dealPriceLB.textColor = [YZMAppStyle colorForMainTextColor];
        }
        
    } else {
        
        for (int i = 0; i < self.contentArray.count; i++) {
            
            UILabel *contentLB = self.contentArray[i];
            contentLB.text = @"--";
            contentLB.textColor = [YZMAppStyle colorForLightGrayText];
        }
    }
}

- (void)compareValue:(NSNumber *)beforeValue withAfter:(NSNumber *)afterValue withIndex:(NSInteger)index {

    UILabel *contentLB = self.contentArray[index];
    
    if (!beforeValue) {
        
        contentLB.text = [NSString stringWithFormat:@"%@", afterValue.p02fString];
        contentLB.textColor = [YZMAppStyle colorForMainTextColor];
        return;
    }
    
    if ([beforeValue isEqualGreaterThanDouble:[afterValue doubleValue]]) {
        
        
        contentLB.text = [NSString stringWithFormat:@"%@", beforeValue.p02fString];
        contentLB.textColor = [YZMAppStyle colorForRiseText];
    } else {
        
        contentLB.text = [NSString stringWithFormat:@"%@", beforeValue.p02fString];
        contentLB.textColor = [YZMAppStyle colorForDropText];
    }
}

@end
